package mobidever.mygodutch.adapter;

import java.util.List;

import android.R.integer;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import mobidever.mygodutch.R;
import mobidever.mygodutch.adapter.base.AdapterBase;
import mobidever.mygodutch.bussiness.BussinessCategory;
import mobidever.mygodutch.bussiness.BussinessPayout;
import mobidever.mygodutch.bussiness.BussinessUser;
import mobidever.mygodutch.controls.SliderItem;
import mobidever.mygodutch.model.ModelCategory;
import mobidever.mygodutch.model.ModelPayout;
import mobidever.mygodutch.utility.DateTools;

public class AdapterPayout extends AdapterBase
{
	BussinessPayout mBussinessPayout;
	BussinessCategory mBussinessCategory;
	private int mAccountBookID;
	
	private class Holder
	{
		ImageView ivIcon;
		TextView tvPayoutName;
		TextView tvTotal;
		TextView tvPayoutuserAndPayoutType;
		
		View RelativeLayoutDate;
	}

	public AdapterPayout(Context pContext,int pAccountBookID)
	{
		super(null, pContext);
		mAccountBookID = pAccountBookID;
		mBussinessPayout = new BussinessPayout(pContext);
		mBussinessCategory = new BussinessCategory(pContext);
		List<ModelPayout> _List = mBussinessPayout.getPayoutByAccountBookID(mAccountBookID);
		setList(_List);
	}
	
	
	public  AdapterPayout(List pList,Context pContext)
	{
		super(pList, pContext);
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		
		
		
		Holder _Holder;
		if (convertView == null)
		{
			convertView = getLayoutInflater().inflate(R.layout.payout_list_item,
					null);

			_Holder = new Holder();

			_Holder.ivIcon = (ImageView) convertView.findViewById(R.id.ivPayoutIcon);
			_Holder.tvPayoutName = (TextView) convertView.findViewById(R.id.tvPayoutName);
			_Holder.tvTotal = (TextView) convertView.findViewById(R.id.tvTotal);
			_Holder.tvPayoutuserAndPayoutType = (TextView) convertView.findViewById(R.id.tvPayoutUserAndPayoutType);
			_Holder.RelativeLayoutDate = convertView.findViewById(R.id.RelativeLayoutDate);
			
			convertView.setTag(_Holder);
		} else
		{
			_Holder = (Holder) convertView.getTag();
		}
		
		_Holder.RelativeLayoutDate.setVisibility(View.GONE);
		ModelPayout _ModelPayout = (ModelPayout) getItem(position);
		String _PayoutDate = DateTools.getFormatShortTime(_ModelPayout.getPayoutDate());
		
		boolean _IsShow = false;
		
		if (position > 0 )
		{
			ModelPayout _ModelPayoutDateLast = (ModelPayout) getItem(position - 1);
			String _PayoutDateLast = DateTools.getFormatShortTime(_ModelPayoutDateLast.getPayoutDate());
			_IsShow = !_PayoutDate.equals(_PayoutDateLast);
		}
		
		if (_IsShow || position == 0)
		{
			_Holder.RelativeLayoutDate.setVisibility(View.VISIBLE);
			String _Message = mBussinessPayout.getPayoutTotalMessage(_PayoutDate, (int)_ModelPayout.getAccountBookID());
			
			((TextView) _Holder.RelativeLayoutDate.findViewById(R.id.tvPayoutDate)).setText(_PayoutDate);
			((TextView) _Holder.RelativeLayoutDate.findViewById(R.id.tvPayoutTotal)).setText(_Message);
		}
		
		_Holder.ivIcon.setImageResource(R.drawable.payout_small_icon);
		_Holder.tvTotal.setText(_ModelPayout.getAmount().toString());
		ModelCategory _ModelCategory = mBussinessCategory.getModelCategoryByCategoryID((int)_ModelPayout.getCategoryID());
		_Holder.tvPayoutName.setText(_ModelCategory.getCategoryName());
		
		BussinessUser _BussinessUser = new BussinessUser(getContext());
		String _UserName = _BussinessUser.getUsernNameByUserID(_ModelPayout.getPayoutUserID());
		_Holder.tvPayoutuserAndPayoutType.setText(_UserName + " " + _ModelPayout.getPayoutType());
		
		return convertView;
	}

}
